import javax.swing.*;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-07-16
 * Time: 9:08
 */
public class MyArrayList implements IList{
    private int[] elem;
    private int useSize;
    public MyArrayList(){
        this.elem = new int[Constant.DEFAUTL_CAPACITY];
    }
    public boolean isFull(){
        return this.useSize == elem.length;
    }
    private void grow(){
        this.elem = Arrays.copyOf(this.elem,  2 * this.elem.length); //开辟新的空间并拷贝过来
    }
    @Override
    public void add(int data) {
        if(isFull()){   //如果空间满了是需要扩容的
            grow();
        }
        this.elem[this.useSize] = data;
        this.useSize++;
    }
    private void checkPosAdd(int pos, String msg){
        if(pos < 0 || pos > useSize){
            throw new PosWrongFulException();
        }
    }

    @Override
    public void add(int pos, int data) {
        if(isFull()){
            grow();
        }
        String msg = Constant.ADD_POS_WRONGFUL;
        checkPosAdd(pos, msg);
        if(pos == this.useSize){
            this.elem[pos] = data;
            this.useSize++;
            return;
        }
        for(int i = this.useSize - 1; i >= pos; i--){
            this.elem[i + 1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.useSize++;
    }

    @Override
    public boolean contains(int toFind) {
       for(int i = 0; i < this.useSize; i++){
           if(this.elem[i] == toFind){
               return true;
           }
       }
       return false;
    }

    @Override
    public int indexOf(int toFind) {
        for(int i = 0; i < this.useSize; i++){
            if(this.elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    public boolean isEmpty(){
        return this.useSize == 0;
    }
    private void CheckPos(int pos, String msg){
        if(pos < 0 || pos >= this.useSize){
            throw new PosWrongFulException(msg);
        }
    }
    @Override
    public int get(int pos) {
        if(isEmpty()){
            throw new EmptyListException(Constant.EMPTY_LIST);
        }
        String msg = Constant.GET_POS_WRONGFUL + pos;
        CheckPos(pos, msg);
        return elem[pos];

    }

    @Override
    public void set(int pos, int value) {
        if(isEmpty()){
            throw new EmptyListException(Constant.EMPTY_LIST);
        }
        String msg = Constant.SET_POS_ILLEGALITY + pos;
        CheckPos(pos, msg);
        elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        if(isEmpty()){
            throw new EmptyListException(Constant.EMPTY_LIST);
        }
        int index = indexOf(toRemove); //要删除元素的数组下标
        if(index == -1){
            System.out.println("没有你要删除的数据...");
            return;
        }
        for(int i = index; i < this.useSize - 1; i++){
            elem[i] = elem[i + 1];
        }
        this.useSize--;
        //elem[usedSize] = null; 如果是引用类型 需要手动置为空
    }

    @Override
    public int size() {
        return this.useSize;
    }

    @Override
    public void clear() {
//        for(int i = 0; i < this.useSize; i++){
//            elem[i] = null;
//        } //如果元素是类类型(引用类型)就必须把对象设置为空引用，如果直接把有效个数清0覆盖就会导致内存泄露
        this.useSize = 0;
    }

    @Override
    public void display() {
        for(int i = 0; i < this.useSize; i++){
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }
}
